package me.hys.api.user;

import javax.servlet.http.HttpServletRequest;

import me.hys.api.BaseApi;
import me.hys.base.ApiResult;
import me.hys.cons.ApiStatus;
import me.hys.entity.doctor.DoctorEntity;
import me.hys.entity.doctor.PatientEntity;
import me.hys.entity.system.UserEntity;
import me.hys.kits.StrKit;
import me.hys.kits.UploadKit;
import me.hys.service.doctor.IDoctorManager;
import me.hys.service.doctor.IPatientManager;
import me.hys.service.system.IUserManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-26 下午8:16:37 . <br>
 * Function:
 */
@Controller
@RequestMapping("/api/user")
public class UserApi extends BaseApi {
	@Autowired
	private IUserManager userManager;
	@Autowired
	private IDoctorManager doctorManager;
	@Autowired
	private IPatientManager patientManager;

	/**
	 * @Title : auth
	 * @Description : 用户验证
	 */
	@ResponseBody
	@RequestMapping(value = "/auth", method = RequestMethod.POST)
	public ApiResult auth(@ModelAttribute UserEntity user) {
		return userManager.authUser(user);
	}

	/**
	 * @Title : changePassword
	 * @Description : 修改密码
	 */
	@ResponseBody
	@RequestMapping(value = "/change_psw", method = RequestMethod.POST)
	public ApiResult changePassword(@RequestParam String userid,
			@RequestParam String oldPwd, @RequestParam String newPwd) {
		return userManager.changePsw(userid, oldPwd, newPwd);
	}

	/**
	 * @Title : get
	 * @Description : 获取个人基本信息
	 */
	@ResponseBody
	@RequestMapping(value = "/get/{userid}", method = RequestMethod.GET)
	public UserEntity get(@PathVariable String userid) {
		return userManager.findById(userid);
	}

	/**
	 * @Title : getDetail
	 * @Description : 获取医生详细信息（医生）
	 */
	@ResponseBody
	@RequestMapping(value = "/get_detail/{userid}", method = RequestMethod.GET)
	public DoctorEntity getDetail(@PathVariable String userid) {
		return doctorManager.findById(userid);
	}

	/**
	 * @Title : getDetailPatient
	 * @Description : 获取患者详细信息
	 */
	@ResponseBody
	@RequestMapping(value = "/get_detail_patient/{userid}", method = RequestMethod.GET)
	public PatientEntity getDetailPatient(@PathVariable String userid) {
		return patientManager.findById(userid);
	}

	/**
	 * @Title : improve
	 * @Description : 完善信息（医生）
	 */
	@ResponseBody
	@RequestMapping(value = "/improve", method = RequestMethod.POST)
	public ApiResult improve(@RequestParam int userid,
			@ModelAttribute DoctorEntity entity) {
		entity.setUserid(userid);
		entity.setNickname(request.getParameter("nickname"));
		return doctorManager.saveOrUpdate(entity);
	}

	/**
	 * @Title : improve
	 * @Description : 完善信息（患者）
	 */
	@ResponseBody
	@RequestMapping(value = "/improve_patient", method = RequestMethod.POST)
	public ApiResult improvePatient(@RequestParam int userid,
			@ModelAttribute PatientEntity entity) {
		entity.setUserid(userid);
		return patientManager.saveOrUpdate(entity);
	}

	/**
	 * @Title : register
	 * @Description : 注册
	 */
	@ResponseBody
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ApiResult register(@ModelAttribute UserEntity user) {
		return userManager.saveRegister(user);
	}

	/**
	 * @Title : update
	 * @Description : 修改个人信息
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ApiResult update(@RequestParam int userid,
			@ModelAttribute UserEntity entity) {
		entity.setId(userid);
		return userManager.update(entity);
	}

	/**
	 * @Title : upfile
	 * @Description : 通用上传文件接口
	 */
	@ResponseBody
	@RequestMapping(value = "/upfile", method = RequestMethod.POST)
	public ApiResult upfile(HttpServletRequest request,
			@RequestParam int userid, @RequestParam MultipartFile file) {
		ApiResult apiResult = new ApiResult(ApiStatus.FAILED);
		String path = UploadKit.upload(request, String.valueOf(userid), file);
		if (!StrKit.isBlank(path)) {
			apiResult.setResult(ApiStatus.SUCCESS);
			apiResult.setMsg(path);
		} else {
			apiResult.setMsg("上传出错");
		}
		return apiResult;
	}

	/**
	 * @Title : uploadHead
	 * @Description : 上传用户头像
	 */
	@ResponseBody
	@RequestMapping(value = "/upload_head", method = RequestMethod.POST)
	public ApiResult uploadHead(HttpServletRequest request,
			@RequestParam int userid, @RequestParam MultipartFile head) {
		ApiResult apiResult = new ApiResult(ApiStatus.FAILED);
		String path = UploadKit.uploadHead(request, String.valueOf(userid),
				head);
		if (!StrKit.isBlank(path)) {
			UserEntity entity = new UserEntity();
			entity.setId(userid);
			entity.setHead(path);
			userManager.saveOrUpdate(entity);
			apiResult.setResult(ApiStatus.SUCCESS);
			apiResult.setMsg(entity.getHead());
		} else {
			apiResult.setMsg("上传出错");
		}
		return apiResult;
	}
}
